package com.ujiuye.config;

import org.apache.commons.io.FileUtils;
import org.springframework.http.*;

import javax.xml.ws.Response;
import java.io.File;
import java.io.IOException;

public class DownLoad {
    public static ResponseEntity<byte[]> download(File file) throws IOException {
        //将文件转为二进制字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);
        //获取响应头对象
        HttpHeaders httpHeaders = new HttpHeaders();
        //设置响应头编码格式
        String newName = new String(file.getName().getBytes("utf-8"), "ISO-8859-1");
         //设置弹框下载
        httpHeaders.setContentDispositionFormData("attchment",newName);
        //设置响应类型   得要支持所有文件类型    二进制流的方式
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
       //返回一个ResponseEntity对象并且设置响应码
       return new ResponseEntity<byte[]>(bytes,httpHeaders,HttpStatus.OK);
    }
}
